package nc.ui.test.borrow.ace.handler;

import java.util.HashSet;
import java.util.Set;

import nc.ui.pub.beans.UIRefPane;
import nc.ui.pub.beans.constenum.DefaultConstEnum;
import nc.ui.pub.bill.BillCardPanel;
import nc.ui.pubapp.uif2app.event.IAppEventHandler;
import nc.ui.pubapp.uif2app.event.card.CardBodyBeforeEditEvent;
import nc.vo.pubapp.pattern.pub.SqlBuilder;

public class CardBodyBeforeEditHandler implements IAppEventHandler<CardBodyBeforeEditEvent>{

	@Override
	public void handleAppEvent(CardBodyBeforeEditEvent e) {
		e.setReturnValue(true);
		
		BillCardPanel card = e.getBillCardPanel();
		
		if(e.getKey().equals("pk_bookdoc")){
			Set<String> set = new HashSet<String>();
			
			int rowCount = card.getBillTable().getRowCount();
			
			DefaultConstEnum pk = null;
			
			for(int row = 0; row < rowCount; row ++){
				pk = (DefaultConstEnum) card.getBillModel().getValueObjectAt(row, e.getKey());
				
				if(pk != null) {
					set.add((String) pk.getValue());
				}else{
					set.add("111");
				}
				
				
			}
			
			String[] pks = new String[set.size()];
			set.toArray(pks);
			
			SqlBuilder sqlB = new SqlBuilder();
			sqlB.appendNot(" and pk_bookdoc", pks);
			
			UIRefPane refPane = (UIRefPane) card.getBodyItem(e.getKey()).getComponent();
			
			refPane.getRefModel().addWherePart(sqlB.toString());
			
		}
	}

}
